

const request=require('../../utils/request.js');//获取封装的请求函数
const globleurl = require('../../utils/serverconfig.js').url;
const app=getApp();


Page({

  /**
   * 页面的初始数据
   */
 
  data: {
    inputgetfocus:false,
    inputvalue:'',
    showinput:false,
    commentcount:0,
    wishicon:'icon-zan_reping',
    iscollected:0, //是否收藏
    wishcount:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showToast({
      title: '加载中',
      icon:'loading',
      duration:500
    });
    let _this=this;
    _this.setData({
      id:options.movie_id
    });
    //拿到请求电影的id
    if(options.movie_id){
      //电影相关数据
    _this.requetsmovie(_this);
      //评论相关数据
    _this.requestcommentlist(_this);
    //获取收藏状态
    _this.getCollecteState(_this);
    this.getwishnumber();
  };

  },

  //点击购买
  fbuy(){
    wx.switchTab({
      url: '../cinema/cinema',
    })
  },


  //获取用户收藏状态
    getCollecteState:function(){
      let _this=this;
      let globalOpenid=app.globalData.openid;
      if(globalOpenid){

        let userid;
        request.myrequest('/miniapp/requestuserid',{openid:globalOpenid},'post').then(value1=>{
          userid=value1.data[0].user_id;
          _this.setData({
          userid:userid
          // nickName:res.userInfo.nickName
        });

        return  new Promise((resolve,rejects)=>{
          //获取当前登录用户对电影的收藏状态
          let state=request.myrequest('/miniapp/getcollectstate',{userid:_this.data.userid,movie_id:_this.data.id});
          resolve(state);
      
        })
        })
        .then(value2=>{
          //如果数据库中没有用户收藏的信息
          if(value2.data.length==0){
            async function insertUserCollectInfo(){
              request.myrequest('/miniapp/insertusercollectinfo',{userid:_this.data.userid,movieid:_this.data.id},'post');
            }
            insertUserCollectInfo();
            return;
          };
          let servercollect_id_state=value2.data[0];//服务器传回的收藏状态和id
          let servercollectid=servercollect_id_state.collect_id;
          let servercollectstate=servercollect_id_state.state;//服务器传回的收藏状态
          if(servercollectstate==0){
            //如果服务器传回的状态是未收藏
            _this.setData({
              iscollected:0
            });
          }else{
            _this.setData({
              iscollected:1
            });
          };
        });             
   


      }else{

      }



    },


  //请求电影信息
  requetsmovie:async (_this)=>{
    try {
      let movie=await request.myrequest('/miniapp/movie',{movie_id:_this.data.id});
      if(movie){
        wx.setNavigationBarTitle({
          title: `${movie.data[0].movie_name}`
        })
        movie.data[0].rt=movie.data[0].rt.split("-")[0];
        movie.data[0].img_url=globleurl+movie.data[0].img_url;
        _this.setData({
          movie:movie.data[0]
        });
      };
    } catch (error) {
      _this.requetsmovie(_this)
    };
   
      
  },

  //请求评论列表
  requestcommentlist:async(_this)=>{
 try {
  let conmentlist=await request.myrequest('/miniapp/commentlist',{movie_id:_this.data.id});
  if(conmentlist){
      _this.setData({
        commentlist:conmentlist.data,
        commentcount:conmentlist.data.length
      });
  };
 } catch (error) {
   _this.requestcommentlist(_this);
 }
  },

  //点击评论，添加评论
  addcomment: function(e){
    let _this=this;
    let globalopenid=app.globalData.openid;
    let globaluserInfo=app.globalData.userInfo;


    if(globalopenid){
      //登陆过了
      let userid;
      request.myrequest('/miniapp/requestuserid',{openid:globalopenid},'post').then(value1=>{
        userid=value1.data[0].user_id;
        this.setData({
        userid:userid,
        nickName:globaluserInfo.nickName
      });
      });

      _this.setData({inputgetfocus:true,showinput:true});
    }else{

        //没有登录
        wx.showToast({
          title: '您还没有登录哦，请登录!',
          icon:'none',
          duration:2000
        });
        setTimeout(()=>{
          wx.switchTab({
            url: '../individual/individual'
          })
        },2000);

    }


  },

  starcount:1,
  onStarChange(e){
    e.detail=e.detail*2;
    this.starcount=e.detail;
  },

  //提交评论
  submitcomment:async function(e){
    let _this=this;
    try {

      if(this.data.inputvalue!=''&&this.data.userid){
        let date=new Date();
        request.myrequest('/miniapp/addcomment',{movie_id:this.data.id,userid:this.data.userid,inputvalue:this.data.inputvalue,scinput:this.starcount,date:date,nickName:this.data.nickName},'post');
        this.setData({
          showinput:false,inputvalue:''
        });
        this.starcount=1;
        let p1=new Promise((resolve,rejects)=>{
          wx.showToast({
            title: '提交成功',
            icon:'success',
            duration:3000,
            success:function(){
              resolve();
            },
            fail:function(){
              rejects();
            }

          }); 
        });
        _this.requestcommentlist(_this);   
      }else{
        wx.showToast({
          title: '您没有填写评论哦！',
          icon:'none',
          duration:1500
        });
      }
    } catch (error) {
      wx.showToast({
        title: '服务器走丢了哦！请重试！',
        icon:'none',
        duration:1500
      }); 
    }
  
  },

  //监听填写评论内容
  commentkeyinput:function(e){
    this.setData({inputvalue:e.detail.value});
  },



  //关闭评论编辑界面
  closeeditcomment:function(){
    this.setData({
      showinput:false,
      inputvalue:''
    })
  },


//点击收藏功能
  oncollecttap:function(){
    let _this=this;
    let globalOpenid=app.globalData.openid;
    let globaluserInfo=app.globalData.userInfo;
    //如果用户已经点击过评论必然是登录状态
    if(this.data.userid){
      new Promise((resolve,rejects)=>{
        //获取当前登录用户对电影的收藏状态
        let state=request.myrequest('/miniapp/getcollectstate',{userid:this.data.userid,movie_id:this.data.id});
        resolve(state);
      })
      .then(value2=>{
        let servercollect_id_state=value2.data[0];//服务器传回的收藏状态和id
        let servercollectid=servercollect_id_state.collect_id;
        let servercollectstate=servercollect_id_state.state;//服务器传回的收藏状态
        if(servercollectstate==0){
          //如果服务器传回的状态是未收藏，则点击后让其变为收藏状态,同步到服务器
          _this.setData({
            iscollected:1,
            wishcount:this.data.wishcount+1
          });

          return new Promise((resolve,reject)=>{
            request.myrequest('/miniapp/changecollectstate',{state:_this.data.iscollected,collectid:servercollectid})
          });

        }else{
          //已经收藏变成未收藏
          _this.setData({
            iscollected:0,
            wishcount:this.data.wishcount-1
          });

          return new Promise((resolve,reject)=>{
            request.myrequest('/miniapp/changecollectstate',{state:_this.data.iscollected,collectid:servercollectid})
          });
        };
      });
    }else{
      if(globalOpenid){
          //拿到收藏用户的id，name
      let userid;
      request.myrequest('/miniapp/requestuserid',{openid:globalOpenid},'post').then(value1=>{
        userid=value1.data[0].user_id;
        this.setData({
        userid:userid,
        nickName:globaluserInfo.nickName
      });
      return new Promise((resolve,rejects)=>{
        //获取当前登录用户对电影的收藏状态
        let state=request.myrequest('/miniapp/getcollectstate',{userid:this.data.userid,movie_id:this.data.id})
        resolve(state);
      });
      })
      .then(value2=>{
        let servercollect_id_state=value2.data[0];//服务器传回的收藏状态和id
        let servercollectid=servercollect_id_state.collect_id;
        let servercollectstate=servercollect_id_state.state;//服务器传回的收藏状态
        if(servercollectstate==0){
          //如果服务器传回的状态是未收藏，则点击后让其变为收藏状态,同步到服务器
          _this.setData({
            iscollected:1,
            wishcount:this.data.wishcount+1
          });

          return new Promise((resolve,reject)=>{
            request.myrequest('/miniapp/changecollectstate',{state:_this.data.iscollected,collectid:servercollectid})
          });

        }else{
          _this.setData({
            iscollected:0,
            wishcount:this.data.wishcount-1
          });

          return new Promise((resolve,reject)=>{
            request.myrequest('/miniapp/changecollectstate',{state:_this.data.iscollected,collectid:servercollectid})
          });
        };
      });
      }else{
        //未登录
        wx.showToast({
          title: '您还没有登录哦，请登录!',
          icon:'none',
          duration:2000
        });
        setTimeout(()=>{
          wx.switchTab({
            url: '../individual/individual'
          })
        },2000);
      }

    };
    
  },


  //点赞功能
  ontapwish:function(e){
    console.log(e);
    // this.setData({
    //   wishicon:'icon-zan_repingyidian'
    // });
  },

  async getwishnumber(){
    let wishnumber=await request.myrequest('/miniapp/moviewish',{id:this.data.id});
    let wishcount=wishnumber.data[0].count;
    this.setData({wishcount})
 
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   
  }


})